package com.vip.file.config;

import com.vip.file.controller.ResultNew;
import com.vip.file.exception.BaseException;
import com.vip.file.model.response.ErrorCode;
import com.vip.file.model.response.RestResponse;
import com.vip.file.model.response.RestResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理，捕获所有Controller中抛出的异常。
 *
 * @author wgb
 * @date 2019/7/19
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常，其他未处理的异常，按系统异常处理
     */
    @ExceptionHandler(Exception.class)
    public RestResponse<String> exceptionHandler(Exception e) {
        log.error("系统异常", e);
        return RestResponses.newFailResponse(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResultNew exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        return ResultNew.error(ex.getMessage());
    }
}
